﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Infrastructure.Configuration;
namespace Finance.Core.Configuration
{
    /// <summary>
    /// Singleton implementation of XMLConfigFile, representing the specific config file for the app.
    /// </summary>
    public class ConfigManager : ConfigurationManagerBase, IConfigurationSource
    {
        private static readonly ConfigManager settings = new ConfigManager();
        public static ConfigManager Instance { get { return settings; } }
       
        private ConfigManager()
            : base(Path.Combine(Path.GetDirectoryName(typeof(ConfigManager).Assembly.Location), "Finance.WPF.exe"))
        {
            CustomizedLayoutTable = LoadCustomizedLayout() ?? new Hashtable();
        }

        #region IConfigurationSource Members

        public void Add(IConfigurationParameter saveParameter, string sectionName, ConfigurationSection configurationSection)
        {

            return;
        }

        public void AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
        {
            return;
        }

        public new ConfigurationSection GetSection(string sectionName)
        {
            return (ConfigurationSection)base.GetSection(sectionName);
        }

        public void Remove(IConfigurationParameter removeParameter, string sectionName)
        {
            return;
        }

        public void RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
        {
            return;
        }

        #endregion

        public Hashtable CustomizedLayoutTable;

        public void SaveCustomizedLayout()
        {
            try
            {
                string _path = Path.Combine(Path.GetDirectoryName(typeof(ConfigManager).Assembly.Location), "Layout\\CustomizedLayout.xml");
                if (!Directory.Exists(Path.GetDirectoryName(_path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(_path));
                }

                DataSet _ds = new DataSet();
                DataTable _table = new DataTable();
                _table.Columns.Add(new DataColumn("key"));
                _table.Columns.Add(new DataColumn("value"));
                _ds.Tables.Add(_table);

                foreach (DictionaryEntry _entity in CustomizedLayoutTable)
                {
                    DataRow _row = _table.NewRow();
                    _row["key"] = _entity.Key;
                    _row["value"] = _entity.Value;
                    _table.Rows.Add(_row);
                }

                _ds.WriteXml(_path);

            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }
        }

        private Hashtable LoadCustomizedLayout()
        {
            Hashtable _config = new Hashtable();

            try
            {
                string _path = Path.Combine(Path.GetDirectoryName(typeof(ConfigManager).Assembly.Location), "Layout\\CustomizedLayout.xml");
                if (File.Exists(_path))
                {
                    //XmlSerializer _xmlS = new XmlSerializer(CustomizedLayoutTable.GetType());
                    //using (XmlReader _xmlReader = XmlReader.Create(_path))
                    //{
                    //    _config = _xmlS.Deserialize(_xmlReader) as Hashtable;
                    //}
                    DataSet _ds = new DataSet();
                    _ds.ReadXml(_path);
                    foreach (DataRow _row in _ds.Tables[0].Rows)
                    {
                        _config.Add(_row["key"], _row["value"]);
                    }
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }

            return _config;
        }
    }
}
